package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.MerchantInfo;
import com.contest.entity.UserInfo;
import com.contest.service.UserInfoService;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.contest.entity.BettingRecord;
import com.contest.service.BettingRecordService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author liuyf
 * @email 123456@qq.com
 * @date 2018-01-19 14:16:32
 */

@Controller
@RequestMapping("/bettingRecord")
public class BettingRecordController {

    @Autowired
    BettingRecordService bettingRecordService;
    @Autowired
    IdWorker idWorker;

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param, HttpServletRequest request) {
        Object obj = param.get("name");
        Wrapper<BettingRecord> w = new EntityWrapper();
        MerchantInfo mer = (MerchantInfo) request.getSession().getAttribute("user");
        Wrapper<UserInfo> w2 = new EntityWrapper<>();
        w2.addFilter("fd_merchant",mer.getFdId());
        List<UserInfo> list = userInfoService.selectList(w2);
        if(list != null && list.size() >0){
            String st = "";
            for(UserInfo u : list){
                st += u.getFdId() + ",";
            }
            String str = st.substring(1,st.length()-1);
            w.in("fd_user_id",str);
        }

        if(obj != null && obj.toString().trim().length() > 0){
            w.addFilter("fd_game_name={0}",String.valueOf(obj));
        }
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<BettingRecord> mPage = new Page<BettingRecord>(page, rows);
        mPage = bettingRecordService.selectPage(mPage,w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(BettingRecord bettingRecord) {
        bettingRecord.setFdId(idWorker.nextId() + "");
        if (bettingRecordService.insert(bettingRecord)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        BettingRecord bettingRecord = bettingRecordService.selectById(fdId);
        model.addAttribute("bettingRecord", bettingRecord);
        return "bettingRecord/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (bettingRecordService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(BettingRecord bettingRecord) {
        if (bettingRecordService.updateById(bettingRecord)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }

}
